﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;

namespace Catering.Catering_Page.Kufang
{
    public partial class DataStates : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Txt_Date.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM',maxDate:'%y-%M'})");
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            if (Session["PersonnelID"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                if (isAdmin)
                {
                    Bind_Data_to_Controls.Bind_Area_ddl(this.DDL_Area, true, -10, companyid, true, false);
                }
                else
                {
                    Bind_Data_to_Controls.Bind_Area_ddl(this.DDL_Area, false, groupid, companyid, true, false);
                    //Bind_Data_to_Controls.Bind_Class_ddl(DDL_zuoyequ.SelectedItem, DDL_banzu, isAdmin, classid);
                }
            }
            this.DDL_Class.SelectedIndex = 0;
            Bind_Data_to_Controls.Bind_Class_ddl(this.DDL_Area.SelectedItem, this.DDL_Class);
            this.DDL_Class.SelectedIndex = 0;
            this.Txt_Date.Text = DateTime.Now.ToString("yyyy-MM");
            //LinkButton1_Click(null, null);
        }

        protected void DDL_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind_Data_to_Controls.Bind_Class_ddl(this.DDL_Area.SelectedItem, this.DDL_Class);
        }

        /// <summary>
        /// 开始统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            this.Repeater1.DataSource = null;
            string StrGroup = this.DDL_Area.SelectedValue;
            string StrClass = this.DDL_Class.SelectedValue;
            string StrDate = this.Txt_Date.Text;
            string Type = this.DDL_Type.SelectedValue;
            int GroupID = 0;
            int ClassID = 0;
            DateTime SearchDate = DateTime.Now;
            Int32.TryParse(StrGroup, out GroupID);
            Int32.TryParse(StrClass, out ClassID);
            if (string.IsNullOrEmpty(StrDate)) return;
            DateTime.TryParse(StrDate, out SearchDate);

            DataTable dt = new DataTable();
            switch (Type)
            {
                case "0":
                    dt = GetInDBData(GroupID, ClassID, SearchDate);
                    break;
                case "1":
                    dt = GetFromRecordsData(GroupID, ClassID, SearchDate);
                    break;
            }
            this.Repeater1.DataSource = dt;
            this.Repeater1.DataBind();
        }

        public DataTable GetInDBData(int GroupID, int ClassID, DateTime SearchDate)
        {
            string whereClause = " 1=1 ";
            if (GroupID > 0) whereClause += String.Format(" AND GroupID={0} ", GroupID);
            if (ClassID > 0) whereClause += String.Format(" AND ClassID={0} ", ClassID);
            whereClause += String.Format(" AND [Year]={0} AND [Month]={1} ", SearchDate.Year, SearchDate.Month);
            DataTable dt = new Catering_Rules.InDbInfo().GetDataStateByInDbInfoNew(whereClause);
            return dt;
            
        }

        public DataTable GetFromRecordsData(int GroupID, int ClassID, DateTime SearchDate)
        {
            string whereClause = " 1=1 ";
            if (GroupID > 0) whereClause += String.Format(" AND GroupID={0} ", GroupID);
            if (ClassID > 0) whereClause += String.Format(" AND ClassID={0} ", ClassID);
            whereClause += String.Format(" AND [Year]={0} AND [Month]={1} ", SearchDate.Year, SearchDate.Month);
            DataTable dt = new Catering_Rules.FromRecords().GetDataStateByFromRecordsNew(whereClause);            
            return dt;
        }
    }
}